﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using ToolUtil;
using zyconfig;

namespace zymain
{
    public partial class FrmGsBzy : Form
    {
        private string kjm="10001";
        private List<string> kjmList = new List<string>();
        public BindingSource BookData;
        public FrmGsBzy()
        {
            InitializeComponent();
        }

        private void AddSmk()
        {
            this.kjmList.Clear();
            this.smkKay.Items.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.SqlCmd = "select * from 书目数据库定义 where 文献类型='MARC数据' order by 库键码";
                DataTable dt = _open.GetDataTable();
                foreach(DataRow drow in dt.Rows)
                {
                    this.smkKay.Items.Add(drow["书目库名"].ToString().Trim());
                    this.kjmList.Add(drow["库键码"].ToString());
                }
                this.smkKay.SelectedIndex = 0;
                this.kjm = this.kjmList[0];
            }
        }

        private void FrmGsBzy_Load(object sender, EventArgs e)
        {
            this.AddSmk();
            this.yljgxKay.SelectedIndex = 0;
            this.yjsfsKay.SelectedIndex = 0;
            this.ejsfsKay.SelectedIndex = 0;
            this.AddJstj();
        }

        private void AddJstj()
        {
            this.yjstjKay.Items.Clear();
            this.ejstjKay.Items.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.SqlCmd = "select * from 检索途径定义 where 库键码="+ this.kjm +" and 是否显示='1' order by 公共查询浏览顺序";
                DataTable dt = _open.GetDataTable();
                foreach (DataRow drow in dt.Rows)
                {
                    this.yjstjKay.Items.Add(drow["字段名称"].ToString().Trim());
                    this.ejstjKay.Items.Add(drow["字段名称"].ToString().Trim());
                }
                this.yjstjKay.SelectedIndex = 0;
                this.ejstjKay.SelectedIndex = 1;
            }
        }

        private void smk_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.smkKay.SelectedIndex;
            this.kjm = this.kjmList[index].ToString();
            this.AddJstj();
        }

        private void sBtn_Click(object sender, EventArgs e)
        {
            int yjsfs = this.yjsfsKay.SelectedIndex;
            int ejsfs = this.ejsfsKay.SelectedIndex;
            int yljgx = this.yljgxKay.SelectedIndex;
            string sql = " select * from 标准书目源库 where 库键码= " + this.kjm;
            string yst = this.ysTxt.Text.Trim(), est = this.esTxt.Text.Trim();
            string yjstj = this.yjstjKay.Text.Trim(), ejstj = this.ejstjKay.Text.Trim();
            if (yjstj.Equals("标准编码"))
            {
                try
                {
                    yst = StringUtil.BuingISBNSept(StringUtil.BarCodeToISBN(yst.Replace("-", "")));
                    yjstj = "标准编码1";
                }
                catch
                {
                    ShowMessageBox.ShowMessageBoxTimeout("输入的ISBN有误！改用题名检索", "警告", MessageBoxButtons.OK, 3000);
                    yjstj = "题名";
                    this.yjstjKay.SelectedIndex = 1;
                }
            }
            if (ejstj.Equals("标准编码"))
            {
                try
                {
                    est = StringUtil.BuingISBNSept(StringUtil.BarCodeToISBN(est.Replace("-", "")));
                    ejstj = "标准编码1";
                }
                catch
                {
                    ShowMessageBox.ShowMessageBoxTimeout("输入的ISBN有误！改用题名检索", "警告", MessageBoxButtons.OK, 3000);
                    ejstj = "题名";
                    this.ejstjKay.SelectedIndex = 1;
                }
            }
            if (!string.IsNullOrEmpty(yst))
            {
                if (yjsfs == 0)
                {
                    sql += " and (" + yjstj + " ='" + yst + "' ";
                }
                else
                    sql += " and (" + yjstj + " like '%" + yst + "%' ";
                if (!string.IsNullOrEmpty(est))
                {
                    if (yljgx == 0)
                    {
                        if (ejsfs == 0)
                        {
                            sql += " and " + ejstj + " ='" + est + "' ";
                        }
                        else
                            sql += " and " + ejstj + " like '%" + est + "%' ";
                    }
                    else
                    {
                        if (ejsfs == 0)
                        {
                            sql += " or " + ejstj + " ='" + est + "' ";
                        }
                        else
                            sql += " or " + ejstj + " like '%" + est + "%' ";
                    }
                }
                sql += " )";
            }
            else
            {
                if (!string.IsNullOrEmpty(est))
                {
                    if (ejsfs == 0)
                    {
                        sql += " and " + ejstj + " ='" + est + "' ";
                    }
                    else
                        sql += " and " + ejstj + " like '%" + est + "%' ";
                }
            }
            Console.WriteLine(sql);
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.SqlCmd = sql;
                DataTable dr = _open.GetDataTable();
                //var dataRows = dr.AsEnumerable();
                //foreach (var row in dataRows)
                //{
                //    var cellList = row.ItemArray.ToList();
                //    row.ItemArray = cellList.Select(x => x.ToString().Trim()).ToArray();
                //}
                dr = DTable.ViewTrim(dr);
                this.BookData = new BindingSource();
                this.dataGridBook.AutoGenerateColumns = false;
                this.BookData.DataSource = dr;
                //this.BookData.PositionChanged += new EventHandler(this.BookData_PositionChanged);
                this.dataGridBook.DataSource = this.BookData;
                this.dataGridBook.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);//宽度自适应
            }
        }

        private void ys_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                string yst = this.ysTxt.Text.Trim(), est = this.esTxt.Text.Trim();
                if (!string.IsNullOrEmpty(yst) || !string.IsNullOrEmpty(est))
                {
                    this.sBtn.Enabled = true;
                }
                else
                {
                    this.sBtn.Enabled = false;
                }
            }
        }

        private void es_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                string yst = this.ysTxt.Text.Trim(), est = this.esTxt.Text.Trim();
                if (!string.IsNullOrEmpty(yst) || !string.IsNullOrEmpty(est))
                {
                    this.sBtn.Enabled = true;
                }
                else
                {
                    this.sBtn.Enabled = false;
                }
            }
        }
    }
}
